package org.example.web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.example.dao.UserDao;
import org.example.entity.User;
import org.example.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

@Api(tags = "/Resource管理")
@RestController
@Log4j2
@RequestMapping(value = "/resource")
public class UserController {

    @Autowired
    private UserDao userDao;

    @Autowired
    private UserMapper userMapper;

    @ApiOperation(value = "根据Id获取单条记录", notes ="根据Id获取单条记录")
    @GetMapping("/getOne.do")
    public Object getOne(Long id) throws Exception {
        return userDao.getOne(id);
    }

    @ApiOperation(value = "根据Id获取单条记录", notes ="根据Id获取单条记录")
    @GetMapping(value = "/listByName.do",produces = {"application/json; charset=UTF-8"})
    public Object listByName(String name) throws Exception {
        return userMapper.listByName(name);
    }

    @ApiOperation(value = "保存", notes ="保存")
    @GetMapping(value = "/insert.do",produces = {"application/json; charset=UTF-8"})
    public Object insert() throws Exception {
        User user = new User();
        user.setLastName("test" + getNowTime());
        user.setEmail(getRandom() + "test@test.com");
        return userDao.save(user);
    }

    private String getNowTime(){
        // 方法1
        return new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()).toString();
    }

    private String getRandom(){
        return String.valueOf(new Random().nextInt());
    }
}
